using System;
using Sce.PlayStation.Core.Graphics;
using Sce.PlayStation.HighLevel.UI;
using Sce.PlayStation.Core.Imaging;
using VitaReader;

namespace VitaReaderLibPS
{
	public struct UIFontSet
	{
		public UIFont Small;
		public UIFont Middle;
		public UIFont Large;
	};
	
	internal class PSView : IReaderView
	{
		private static GraphicsContext mGraphics;
		private PSReader mOwner;
		private ReaderScene mScene;
		private int mWidth;
		private int mHeight;
		private UIFont mFont;		
		private UIFontSet mFontSet;		
		
		public PSView(PSReader owner)
		{
			mOwner = owner;
		}
		
		void IReaderView.Initialize()
		{
			//mGraphics = new GraphicsContext(960, 544, PixelFormat.Rgba, PixelFormat.Depth24, MultiSampleMode.None);
			mGraphics = new GraphicsContext();
			
			// Initialize UI Toolkit
            UISystem.Initialize(mGraphics);
			
			IReader owner = mOwner as IReader;
			
			mFont = new UIFont("/Application/font/"+owner.FontFile,owner.FontSize, FontStyle.Regular);
			
			mFontSet.Small = new UIFont ("/Application/font/"+owner.FontFile, 15,FontStyle.Regular);
			mFontSet.Middle = new UIFont ("/Application/font/"+owner.FontFile, 25,FontStyle.Regular);
			mFontSet.Large = new UIFont ("/Application/font/"+owner.FontFile, 35,FontStyle.Regular);
			
			mWidth = mGraphics.GetFrameBuffer().Width;
			mHeight = mGraphics.GetFrameBuffer().Height;
			
			// Set scene
			mScene = new ReaderScene(this);
			UISystem.SetScene(mScene, null);		
		}
		
		void IReaderView.Shutdown()
		{
			UISystem.ResetStateAll();
			mGraphics.Dispose();
		}
		
		void IReaderView.Update()
		{
			//
			mScene.Update();
			
			// Clear the screen
			mGraphics.SetClearColor(0.0f, 0.0f, 0.0f, 0.0f);
			mGraphics.Clear();
			
			//draw UI & text
			UISystem.Render();

			// Present the screen
			mGraphics.SwapBuffers();
		}
		
		float IReaderView.GetTextHeight(String text)
		{
			throw new NotImplementedException();
		}
		
		int IReaderView.GetTextWidth(String text)
		{
			return mFont.GetFont().GetTextWidth(text);
		}
		
		int IReaderView.GetCharWidth(char c)
		{
			return mFont.GetFont().GetTextWidth( new String(c,1) );
		}
		
		int IReaderView.GetFontHeight()
		{
			return mFont.GetFont().Metrics.Height + mFont.GetFont().Metrics.Leading;
		}
		

		/*
		CharMetric	IReaderView.GetCharacterMetric(char c,int fontSize)
		{
		}
		*/
		
		//demonsion:width
		int IReaderView.Width				{get {return mWidth;}}
		//demsion:height
		int IReaderView.Height				{get {return mHeight;}}
		
		IReader IReaderView.Owner			{get {return mOwner as IReader;}}
		
		int IReaderView.MaxCharacterPerView	{get {throw new NotImplementedException();}}
		
		float IReaderView.MaxTextHeight		{get {return 2048.0f;}}
		
		//global button size info
		float IReaderView.ButtonPadding		{get {return 5.0f;}}
		
		float IReaderView.ButtonWidth		{get {return mWidth*0.25f;}}
		
		float IReaderView.ButtonHeight		{get {return mHeight*0.13f;}}

		public UIFont Font					{get {return mFont;}}
		
		public UIFontSet FontSet			{get {return mFontSet;}}
		
		public void UpdateFont(bool file_change)
		{
			IReader owner = mOwner as IReader;
			mFont.GetFont().Dispose();
			mFont = new UIFont("/Application/font/"+owner.FontFile, owner.FontSize, FontStyle.Regular);
			if( file_change )
			{
				mFontSet.Small.GetFont().Dispose();
				mFontSet.Middle.GetFont().Dispose();
				mFontSet.Large.GetFont().Dispose();
				
				mFontSet.Small = new UIFont ("/Application/font/"+owner.FontFile, 15,FontStyle.Regular);
				mFontSet.Middle = new UIFont ("/Application/font/"+owner.FontFile, 25,FontStyle.Regular);
				mFontSet.Large = new UIFont ("/Application/font/"+owner.FontFile, 35,FontStyle.Regular);
			}
		}
	}
}

